package com.atguigu.tingshu.handle;

import com.atguigu.tingshu.model.CDCEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @Author zlq
 * @CreateTime 2025/3/5
 */
@Component
@Slf4j
@CanalTable("album_info") //监听变更表
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void insert(CDCEntity cdcEntity) {
        log.info("监听到数据插入,ID:{}", cdcEntity.getId());
        EntryHandler.super.insert(cdcEntity);
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        log.info("监听到数据修改：before:ID:{}", before.getId());
        log.info("监听到数据修改：after:ID:{}", after.getId());
        try {
            //redis，del key
            String dataKey="album:info:"+after.getId();
            redisTemplate.delete(dataKey);
        } catch (Exception e) {
            log.error("更新专辑信息失败：{}",e.getMessage());
            //  sendMsg(exchange,routingkey,after.getId()); 专辑微服务监听;
            System.out.println(e.getMessage());
        }
        EntryHandler.super.update(before, after);
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        log.info("监听到数据删除,ID:{}", cdcEntity.getId());
        EntryHandler.super.delete(cdcEntity);
    }
}
